const query = require("../db/mysql")
const { success, error } = require("../module/module")

class Agent {
    //搜索
    searchAgen = async (ctx) => {
        const { name } = ctx.request.body
        let sql = `select * from agent where 1=1 `
        if (name) {
            sql += `and name like "%${name}%" `
        }
        let data = await query(sql)
        if (data.length) {
            ctx.body = success(data, '搜索成功')
        } else {
            ctx.body = error('没有类似结果')
        }

    }

    //筛选
    agnetSelect = async (ctx) => {
        let { state } = ctx.request.body
        let sql = `select * from agent where 1=1 `
        if (state) {
            sql += `and state="${state}"`
        }
        let data = await query(sql)
        if (data.length) {
            ctx.body = success(data, '查找成功')
        } else {
            ctx.body = error('查找成功')
        }
    }
    //添加
    addAgent = async (ctx) => {
        let { name, phone, company, newBuil, oldBuil, rent, state } = ctx.request.body
        let homeSql = `select * from agent where 1=1  and name like "%${name}%"  and company like "%${company}" `
        let isHave = await query(homeSql)
        if (isHave.length) {
            ctx.body = error('已存在此经纪人')
        } else {
            let sql = `insert into agent (name, phone, company, newBuil, oldBuil, rent, state) values ("${name}","${phone}","${company}","${newBuil}","${oldBuil}","${rent}","${state}") `
            let data = await query(sql)
            if (data.affectedRows === 1) {
                ctx.body = success(data, '添加成功')
            } else {
                ctx.body = error('添加失败')
            }
        }
    }
    //编辑
    changeAgent = async (ctx) => {
        let { id, name, phone, company, newBuil, oldBuil, rent, state } = ctx.request.body
        let sql = `update agent set name='${name}' , phone='${phone}',company='${company}',newBuil='${newBuil}' ,oldBuil='${oldBuil}', rent='${rent}' ,state='${state}' where id=${id} `
        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '更新成功')
        } else {
            ctx.body = error('更新失败')
        }
    }

    //删除
    deleteAgent = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `delete  from agent where 1=1 and id=${id} `
        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '删除成功')
        } else {
            ctx.body = error('删除失败')
        }
    }

    //获取数据
    getAgent = async (ctx) => {
        let { pageIndex, pageSize } = ctx.query
        let sql = `select * from agent where 1=1 `
        let countSql = 'select count(id) as total from agent '
        let total = await query(countSql)
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex * 1 - 1) * pageSize * 1},${pageSize * 1} `
        }
        let data = await query(sql)
        ctx.body = {
            total: total[0].total,
            data,
            code: 1
        }
    }

}

module.exports = new Agent()